<!DOCTYPE html>
<html  class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>PingPangTask-0.1</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <base th:href="@{/}">
        <link rel="stylesheet" href="static/css/font.css">
        <link rel="stylesheet" href="static/css/xadmin.css">
        <script src="static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="static/js/xadmin.js"></script>
    </head>
    
    <body>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        
                            <form class="layui-form layui-col-space5" id="sreach">
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" placeholder="开始日" name="startDate" id="start"></div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input class="layui-input" placeholder="截止日" name="endDaet" id="end"></div>
								<div class="layui-input-inline layui-show-xs-block">
                                    <select name="status">
                                        <option value="">所有任务</option>
                                        <option value="0">待执行</option>
                                        <option value="1">正在执行</option>
                                        <option value="2">暂停</option>
                                        <option value="-1">删除</option>
                                    </select>
                                </div>
								<div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" name="registIPPort" placeholder="IP" autocomplete="off" class="layui-input"></div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <input type="text" name="name" placeholder="请输入任务名称" autocomplete="off" class="layui-input"></div>
                                <div class="layui-input-inline layui-show-xs-block">
                                    <button class="layui-btn" type="button"  onclick="sreach();">
                                        <i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()">
                                <i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加任务','taskManager/add?id=-1',400,500)">
                                <i class="layui-icon"></i>添加</button></div>
                                
                        <div class="layui-card-body ">
                            <table class="layui-table layui-form" id="run">
                            </table>
                        </div>       
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
    
    layui.use(['laydate','form','table'],
            function() {
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#start' //指定元素
                });

                //执行一个laydate实例
                laydate.render({
                    elem: '#end' //指定元素
                });
                
                var table = layui.table;
                //第一个实例
                table.render({
                  elem: '#run',
                  height: 312,
                  url: 'taskManager/data', //数据接口
                  method:'post',
                  contentType: 'application/json; charset=UTF-8',
                  //where:JSON.stringify(layui.form.field),
                  page: true, //开启分页
                  cols: [
                	      [
                	    	{type: 'checkbox',fixed:'left'},
    		                {field: 'id', title: 'ID', width:50, sort: true},
    		                {field: 'name', title: '任务名称', width:80},
    		                {field: 'purpose', title: '备注', width:80},
    		                {field: 'registName', title: '注解名称', width:80},
    		                {field: 'registMethod', title: '执行方法', width:120},
    		                {field: 'registIPPort', title: 'IP端口', width:100},
    		                {field: 'corn', title: 'CORN表达式', width:100},
    		                {field: 'param', title: '参数', width:80},
    		                //{field: 'insertDate', title: '插入时间', width: 150},
    		                //{field: 'updateDate', title: '更新时间', width: 150},
    		                {field: 'status', title: '状态', width: 80,templet:'<div>{{ statusFormat(d.status)}}</div>'},
    		                {field: 'right', title: '操作', width: 370, toolbar:"#barDemo"}
                          ]
                	    ]
                });
                
            });
    function sreach(){
    	var param=JSON.stringify(getFormJson($('#sreach')));
    	//alert(param);
    	layui.table.reload("run", { //此处是上文提到的 初始化标识id
            where: {
            	search:param
            }
        });
    	return false;
    }
    

    //将form中的值转换为键值对。
    function getFormJson(frm) {
        var o = {dosubmit:1};
        var a = $(frm).serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
    
    /*用户-删除*/
    function member_del(id,status){
    	
      if("0"!=status && "-1"!=status){
   		  layer.msg('存在已执行的任务!', {icon: 1});
   		  return false;
   	  }
    	
        layer.confirm('确认要删除吗？',function(index){
        	
        	  $.ajax({
        		  type: 'POST',
        		  async: false,
        		  dataType:'json',
        		  url:'taskManager/del',
        		  data:"ids="+id,
        		  success: function(data){
        			  if("S"==data.CODE){
        				  layer.msg('删除成功', {icon: 1});
        				  layui.table.reload("run", { //此处是上文提到的 初始化标识id
        		                where: {
        		                    //key: { //该写法上文已经提到
        		                        //type: item.type, id: item.id
        		                    //}
        		                }
        		            });
        			  }
        		  },
        		  error : function(errorMsg) {
                      layer.msg(errorMsg,{time:2000,end:function(){
        			  }});
        		  }
        		});
        });
    }
    
    /*批量-删除*/
    function delAll () {
     var checkStatus = layui.table.checkStatus('run').data;
     if(null==checkStatus || checkStatus.length<=0){
    	 layer.msg('请选择数据!', {icon: 1});
		  return false;
     }
      var ids = [];
      for(var i=0;i<checkStatus.length;i++){
    	  if("0"!=checkStatus[i].status && "-1"!=checkStatus[i].status){
    		  layer.msg('存在已执行的任务!', {icon: 1});
    		  return false;
    	  }
    	  ids.push(checkStatus[i].id);
      }
      layer.confirm('确认要删除吗？',function(index){
          //捉到所有被选中的，发异步进行删除
           $.ajax({
            		  type: 'POST',
            		  async: false,
            		  dataType:'json',
            		  url:'taskManager/del',
            		  data:"ids="+ids.toString(),
            		  success: function(data){
            			  if("S"==data.CODE){
            				  layer.msg('删除成功', {icon: 1});
            				  layui.table.reload("run", { //此处是上文提到的 初始化标识id
            		                where: {
            		                    //key: { //该写法上文已经提到
            		                        //type: item.type, id: item.id
            		                    //}
            		                }
            		            });
            			  }else{
            				  layer.msg(data.MESSAGE, {icon: 1});
            			  }
            		  },
            		  error : function(errorMsg) {
                          layer.msg(errorMsg,{time:2000,end:function(){
            			  }});
            		  }
            		});
      }); 
    }
 
    
    /**
     *type 1:添加执行 2:触发 3:暂停 4:任务删除
     */
    function managerJob(id,type){
    	 var msg="";
    	 var url="";
    	 if(1==type){
    		 msg="确认要执行吗？";
    		 url="jobManager/addJob";
    	 }else if(2==type){
    		 msg="确认要触发吗？";
    		 url="jobManager/runJob";
    	 }else if(3==type){
    		 msg="确认要暂停吗？";
    		 url="jobManager/pauseJob";
    	 }else if(4==type){
    		 msg="确认要删除吗？";
    		 url="jobManager/deleteJob";
    	 }else{
    		 layer.msg('操作未识别!', {icon: 1});
   		     return false;
    	 }
    	 layer.confirm(msg,function(index){
       	  $.ajax({
       		  type: 'POST',
       		  async: false,
       		  dataType:'json',
       		  url:url,
       		  data:"id="+id,
       		  success: function(data){
       			  if("S"==data.CODE){
       				  layer.msg('执行成功', {icon: 1});
       				 // window.parent.location.reload();
       				  layui.table.reload("run", { //此处是上文提到的 初始化标识id
       		                where: {
       		                    //key: { //该写法上文已经提到
       		                        //type: item.type, id: item.id
       		                    //}
       		                }
       		            });
       			  }else{
       				if(null==data.MESSAGE){
       				  layer.msg('执行失败', {icon: 1});
       				}else{
       				  layer.msg(data.MESSAGE, {icon: 1});
       				}
       			  }
       		  },
       		  error : function(errorMsg) {
                     layer.msg(errorMsg,{time:2000,end:function(){
       			  }});
       		  }
       		});
       });
    }    
    
function statusFormat(statu) {
	var msg="";
    if(0==statu){
    	msg="待执行";
    }else if(1==statu){
    	msg="正在执行";
    }else if(2==statu){
    	msg="暂停";
    }else if(-1==statu){
    	msg="删除";
    }else{
    	msg=" 未识别 ";
    }
    return msg;
}  
</script>
<script type="text/html" id="barDemo">
       {{#  if(d.status == 0 || d.status == -1){ }}
	    <button type="button" class="layui-btn" onclick="xadmin.open('编辑','taskManager/add?id={{d.id}}');">编辑</button>
        <button type="button" class="layui-btn" onclick="member_del('{{d.id}}','{{d.status}}');">删除</button>
		{{#  } }}
        <button type="button" class="layui-btn layui-btn-danger" onclick="managerJob('{{d.id}}','1')">执行</button>
		<button type="button" class="layui-btn layui-btn-danger" onclick="managerJob('{{d.id}}','2')">触发</button>
		<button type="button" class="layui-btn layui-btn-danger" onclick="managerJob('{{d.id}}','3')">暂停</button>
        <button type="button" class="layui-btn layui-btn-danger" onclick="managerJob('{{d.id}}','4')">任务删除</button>
</script>
</html>